/**
 * @desc
 * 1. 创建HTML PC首页的, 内页的 微信首页内页的.
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    16/11/15
 **/
'use strict';

var request = require('request');
let ConstDefaultServerHost = "http://172.21.4.63:3333";
var fse = require('fs-extra');
var path = require('path');
var tplUtil = require('@qianmi/x-site-common/util/template-util');

module.exports = ({workBench, serverHost})=> {

    if (!serverHost) {
        serverHost = ConstDefaultServerHost
    }
    console.log('加载文件pc 主页html');
    request(serverHost + "/resources/pc/index/editor/html", function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('加载文件pc 主页html结束');
            fse.outputFile(path.join(workBench, './views/prototype.html'), body);
        }
    });

    console.log('加载文件pc 内页html');
    request(serverHost + "/resources/pc/inner/editor/html", function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('加载文件pc 内页html结束');
            fse.outputFile(path.join(workBench, './views/inner.html'), body);
        }
    });

    console.log('加载文件微信 主页html');
    request(serverHost + "/resources/weixin/index/editor/html", function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('加载文件微信 主页html结束');
            fse.outputFile(path.join(workBench, './views/weixin.html'), body);
        }
    });

    console.log('加载文件微信 内页html');
    request(serverHost + "/resources/weixin/inner/editor/html", function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('加载文件微信 内页html结束');
            fse.outputFile(path.join(workBench, './views/weixinContent.html'), body);
        }
    });

    //获取eslint配置文件
    // console.log('加载eslint配置文件。。。');
    // request(serverHost + "/resources/eslint/src", function (error, response, body) {
    //     if (!error && response.statusCode === 200) {
    //         fse.outputFile(path.join(workBench, './.eslintrc.js'), body);
    //     }
    // });
    // request(serverHost + "/resources/eslint/ignore", function (error, response, body) {
    //     if (!error && response.statusCode === 200) {
    //         fse.outputFile(path.join(workBench, './.eslintignore'), body);
    //     }
    // });
    // request(serverHost + "/resources/eslint/editorCf", function (error, response, body) {
    //     if (!error && response.statusCode === 200) {
    //         fse.outputFile(path.join(workBench, './.editorconfig'), body);
    //     }
    // });
    // console.log('加载eslint配置文件完成');

    //template-index.json 下载
    let platForms = ['weixin', 'pc', 'app'];
    for (let i = 0, ilen = platForms.length; i < ilen; i++) {
        console.log('下载平台模板索引文件', platForms[i]);
        request(serverHost + "/resources/" + platForms[i] + "/templateIndex", function (error, response, body) {
            if (!error && response.statusCode == 200) {
                console.log('下载平台模板索引文件', platForms[i], '结束');
                fse.outputFile(path.join(workBench, './template-index/' + platForms[i] + '-template-index.json'), body);
                fse.outputFile(path.join(workBench, './template-index/' + platForms[i] + '-template-index.js'), tplUtil.transfertpIndex2JsFile(JSON.parse(body)));
            }
        });
    }

    //修改package.json
    let packageJson = fse.readJsonSync(path.join(workBench, './package.json'));
    if (packageJson && packageJson.scripts) {
        packageJson.scripts.prepublish = "node ./node_modules/@qianmi/x-site-tpl-develop/bin/pre-publish";
        packageJson.scripts.postpublish = "node ./node_modules/@qianmi/x-site-tpl-develop/bin/post-publish";
    } else if (packageJson && !packageJson.scripts) {
        packageJson.scripts = {
            "prepublish": "node ./node_modules/@qianmi/x-site-tpl-develop/bin/pre-publish",
            "postpublish": "node ./node_modules/@qianmi/x-site-tpl-develop/bin/post-publish"
        }
    }
    fse.outputJson(path.join(workBench, './package.json'), packageJson);

};


